#ifndef _CANARY2_CORO_EPOLL_SUSPEND_MANAGER_H_
#define _CANARY2_CORO_EPOLL_SUSPEND_MANAGER_H_

#include <coroutine>
#include "../sys/posix_pipe.h"
#include "../sys/poller.h"
#include <memory>
#include "../sys/task_manager.h"
#include "EPSC_manager.h"

namespace wilson::coro
{
    class epoll_suspend_manager
    {
    public:
        epoll_suspend_manager(size_t thrcnt);
        ~epoll_suspend_manager();

        // called by sleeper
        void wait_and_schedule(::std::chrono::milliseconds timeout) noexcept; 
        void add_task(int fd, uint32_t event, ::std::coroutine_handle<> h);
        void stop();
        void awake(size_t cnt = 1) noexcept;
        sys::normal_task_mgr& task_manager() { return *esm_task_mgr; }
        bool done() const noexcept { return esm_task_mgr->empty(); }
        
    private:
        ::std::shared_ptr<sys::normal_task_mgr> esm_task_mgr{ 
            sys::normal_task_mgr::get_from_global()
        };
        size_t          esm_managed_thr_cnt;
        EPSC_manager    esm_suspended_ctxs;
        sys::posix_pipe esm_pipe;
        sys::poller     esm_poller;
    };
}

#endif
